﻿using System.Collections.Generic;
using System.Linq;

namespace TestUp.Models.Helpers
{
    public class GroupHelper
    {
        public List<Group> GetAllGroups()
        {
            return DataWorkItem.Entity.Groups.ToList();
        }

        public Group GetGroup(int groupId)
        {
            return DataWorkItem.Entity.Groups.FirstOrDefault(x => x.GroupId == groupId);
        }

        public void DeleteGroup(int groupId)
        {
            var group = GetGroup(groupId);

            if(group!=null)
            {
                DataWorkItem.Entity.Groups.Remove(group);
                DataWorkItem.Entity.SaveChanges();
            }
        }

        public void UpdateGroup(Group group)
        {
            var originalGroup = GetGroup(group.GroupId);
            if (originalGroup != null)
            {
                //Update fields
                originalGroup.GroupName = group.GroupName;
            }
            DataWorkItem.Entity.SaveChanges();
        }

        public void CreateGroup(string groupName)
        {
            Group group = new Group
                {
                    GroupName = groupName
                };
            DataWorkItem.Entity.Groups.Add(group);
            DataWorkItem.Entity.SaveChanges();
        }
    }
}